package org.malred.dao;

import org.malred.io.Resources;
import org.malred.pojo.User;
import org.malred.sqlSession.SqlSession;
import org.malred.sqlSession.SqlSessionFactory;
import org.malred.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    @Override
    public int update(User u) {
        return 0;
    }

    @Override
    public int delete(User u) {
        return 0;
    }

    @Override
    public void insert(User u) {

    }

    @Override
    public List<User> findAll() {
        InputStream resourceAsStream =
                Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        List<User> users = sqlSession.selectList("user.selectList");
        return users;
    }

    @Override
    public User findByCondition(User u) {
        System.out.println("before");
        InputStream resourceAsStream =
                Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        User user = new User();
        user.setId(1);
        user.setUsername("张三");
        User one = sqlSession.selectOne("user.selectOne", user);

        return one;
    }
}
